package cn.iocoder.yudao.module.hs.controller.admin.rfid;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.module.hs.controller.admin.rfid.vo.RfidChangeReqVO;
import cn.iocoder.yudao.module.hs.controller.admin.rfid.vo.RfidPageReqVO;
import cn.iocoder.yudao.module.hs.controller.admin.rfid.vo.RfidRespVO;
import cn.iocoder.yudao.module.hs.controller.admin.rfid.vo.RfidSaveReqVO;
import cn.iocoder.yudao.module.hs.dal.dataobject.rfid.RfidDO;
import cn.iocoder.yudao.module.hs.service.rfid.RfidService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;

import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.EXPORT;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - RFID信息")
@RestController
@RequestMapping("/hs/rfid")
@Validated
public class RfidController {

    @Resource
    private RfidService rfidService;

    @PostMapping("/create")
    @Operation(summary = "创建RFID信息")
    @PreAuthorize("@ss.hasPermission('hs:rfid:create')")
    public CommonResult<Long> createRfid(@Valid @RequestBody RfidSaveReqVO createReqVO) {
        return success(rfidService.createRfid(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新RFID信息")
    @PreAuthorize("@ss.hasPermission('hs:rfid:update')")
    public CommonResult<Boolean> updateRfid(@Valid @RequestBody RfidSaveReqVO updateReqVO) {
        rfidService.updateRfid(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除RFID信息")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('hs:rfid:delete')")
    public CommonResult<Boolean> deleteRfid(@RequestParam("id") Long id) {
        rfidService.deleteRfid(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得RFID信息")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('hs:rfid:query')")
    public CommonResult<RfidRespVO> getRfid(@RequestParam("id") Long id) {
        RfidDO rfid = rfidService.getRfid(id);
        return success(BeanUtils.toBean(rfid, RfidRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得RFID信息分页")
    @PreAuthorize("@ss.hasPermission('hs:rfid:query')")
    public CommonResult<PageResult<RfidRespVO>> getRfidPage(@Valid RfidPageReqVO pageReqVO) {
        PageResult<RfidDO> pageResult = rfidService.getRfidPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, RfidRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出RFID信息 Excel")
    @PreAuthorize("@ss.hasPermission('hs:rfid:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportRfidExcel(@Valid RfidPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<RfidDO> list = rfidService.getRfidPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "RFID信息.xls", "数据", RfidRespVO.class,
                        BeanUtils.toBean(list, RfidRespVO.class));
    }

    @PostMapping("/change")
    @Operation(summary = "更换标签")
    @PreAuthorize("@ss.hasPermission('hs:rfid:change')")
    public CommonResult<Boolean> change(@Valid @RequestBody RfidChangeReqVO changeReqVO) {
        rfidService.changeRfid(changeReqVO.getOldRfidCode(), changeReqVO.getNewRfidCode());
        return success(true);
    }

    @PostMapping("/bind")
    @Operation(summary = "绑定托盘和RFID信息")
    @PreAuthorize("@ss.hasPermission('hs:rfid:create')")
    public CommonResult<Boolean> createTray(@Valid @RequestBody RfidSaveReqVO createReqVO) {
        rfidService.bind(createReqVO.getTrayCode(), createReqVO.getCode());
        return success(true);
    }

    @GetMapping("/getLowBatteryCount")
    @Operation(summary = "获得低电量RFID数量")
    @PreAuthorize("@ss.hasPermission('hs:rfid:query')")
    public CommonResult<Integer> getLowBatteryCount() {
        return success(rfidService.getLowBatteryCount());
    }
}